﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityTypes;
using System.Data.Objects;
using System.Data.Objects.DataClasses;


namespace SI2_TrabalhoFinal
{
    public partial class HistorialClinico : Form
    {
        SI2FinalEntities entities = Connection.entities;

        public HistorialClinico()
        {
            InitializeComponent();
        }

        private void HistorialClinico_Load(object sender, EventArgs e)
        {
            var query = from p in this.entities.Pessoa.Include("Paciente").Where(p => p.Paciente.Count != 0)
                        select p;
            try
            {
                this.PacienteHistorial.DisplayMember = "Nome";
                this.PacienteHistorial.DataSource = ((ObjectQuery)query).Execute(MergeOption.AppendOnly);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.PacienteHistorial.SelectedItem == null)
            {
                MessageBox.Show("Seleccione um Paciente");
                return;
            }
            if (this.InitHistorial.Value > DateTime.Now)
            {
                MessageBox.Show("Data de Início Inválida");
                return;
            }
            if (this.FimHistorial.Value > DateTime.Now)
            {
                MessageBox.Show("Data de Fim Inválida");
                return;
            }
            if(this.InitHistorial.Value>this.FimHistorial.Value)
            {
                MessageBox.Show("Data de Fim não pode ser inferior à de Início");
                return;
            }
            this.ListarHistorial.Visible = true;
            this.ListarHistorial.Enabled = true;
            this.ConteudoHistorial.Visible = true;
            this.ConteudoHistorial.Enabled = true;
            this.ConteudoHistorial.Clear();

            Pessoa pessoa = (Pessoa)(this.PacienteHistorial.SelectedItem);

            var queryconsulta = from p in entities.Paciente
                                where p.IdPessoa == pessoa.NumeroSS
                                from consulta in entities.Consulta
                                where (consulta.IdPaciente == p.NumeroBenefeciario &&
                                (consulta.Data >= this.InitHistorial.Value.Date ||
                                consulta.Data <= this.FimHistorial.Value.Date))
                                from relatorio in entities.Relatorio
                                where relatorio.IdConsulta == consulta.IdConsulta                                
                                from relatorioMedico in entities.RelatorioMedico
                                where relatorioMedico.IdRelatorio == relatorio.IdRelatorio
                                from relatorioExame in entities.RelatorioExame
                                where relatorioExame.IdRelatorio == relatorio.IdRelatorio && relatorioExame.EquipamentoNormalizado == true
                                select new { Consulta=consulta, RelatorioMedico=relatorioMedico, RelatorioExame=relatorioExame};
            
             this.ListarHistorial.BeginUpdate();
            
            foreach (var a in queryconsulta) { 
                TreeNode c = new TreeNode();
                c.Text = "Consulta "+a.Consulta.Data.Date;
                c.Tag = a.Consulta;
                if (a.RelatorioMedico!=null){
                    TreeNode rm = new TreeNode("Relatório Médico");
                    rm.Tag=a.RelatorioMedico;
                    c.Nodes.Add(rm);
                }
                if (a.RelatorioExame!=null){
                    TreeNode re = new TreeNode("Relatório Exame");
                    re.Tag=a.RelatorioExame;
                    c.Nodes.Add(re);
                }
                this.ListarHistorial.Nodes.Add(c);
            }
            this.ListarHistorial.EndUpdate();

        }

        private void PacienteHistorial_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ListarHistorial.Visible = false;
            this.ListarHistorial.Enabled = false;
            this.ConteudoHistorial.Visible = false;
            this.ConteudoHistorial.Enabled = false;
        }

        private void PacienteHistorial_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.ListarHistorial.Visible = false;
            this.ListarHistorial.Enabled = false;
            this.ConteudoHistorial.Visible = false;
            this.ConteudoHistorial.Enabled = false;
        }

        private void ListarHistorial_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this.ConteudoHistorial.Clear();
            TreeNode node = this.ListarHistorial.SelectedNode;
            if (node == null)
                return;
            Object value = node.Tag;

            if (value.GetType() == typeof(RelatorioExame))
            {
                this.ConteudoHistorial.Text =
                    "Data Exame: " + ((RelatorioExame)value).Relatorio.Data.Date + ". \r\n" +
                    "Tipo de Exame: " + ((RelatorioExame)value).TipoExame.Nome + ". \r\n" +
                    "ID Equipamento: " + ((RelatorioExame)value).IdEquipamento + ". \r\n" +
                    "Descrição: " + ((RelatorioExame)value).Relatorio.Descricao + ". \r\n" +
                    "Notas: " + ((RelatorioExame)value).Notas;
                return;
            }
            if (value.GetType() == typeof(RelatorioMedico))
            {
                this.ConteudoHistorial.Text =
                    "Data Exame: " + ((RelatorioMedico)value).Relatorio.Data.Date + ". \r\n" +
                    "Descrição: " + ((RelatorioMedico)value).Relatorio.Descricao + ". \r\n" +
                    "Estado Clínico: " + ((RelatorioMedico)value).EstadoClinico + ". \r\n" +
                    "Prescrições: " + ((RelatorioMedico)value).Prescricoes;
                return;
            }
        }


    }
}
